using System;
using UnityEngine;
using HutongGames.PlayMaker;

namespace PixelCrushers.DialogueSystem.PlayMaker
{

    [ActionCategory("Dialogue System")]
    [HutongGames.PlayMaker.TooltipAttribute("Saves a game to a string variable.")]
    public class SaveGame : FsmStateAction
    {

        [RequiredField]
        [UIHint(UIHint.Variable)]
        [HutongGames.PlayMaker.TooltipAttribute("Store the result in a String variable")]
        public FsmString storeResult;

        public override void Reset()
        {
            storeResult = null;
        }

        public override void OnEnter()
        {
            if (storeResult != null)
            {
                var saveSystem = GameObject.FindObjectOfType<SaveSystem>();
                if (saveSystem != null)
                {
                    storeResult.Value = JsonUtility.ToJson(SaveSystem.RecordSavedGameData());
                }
                else
                {
                    storeResult.Value = PersistentDataManager.GetSaveData();
                }
            }
            Finish();
        }

    }

}